package com.xxx.video.dynamic.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.dynamic.beans.ChatListBean;

import butterknife.BindView;

public class ChatListAdapter extends BaseRecycleViewAdapter<ChatListBean.PageBean.ListBean, BaseViewHolder> {
    private String userId;
    private String userAvatar;

    public ChatListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder onCreateDefaultViewHolder(View view, int viewType) {
        if (viewType == 1) {
            return new FromChatHolder(view);
        }
        return new SendChatHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        if (viewType == 1) {
            return R.layout.item_chat_from;
        }
        return R.layout.item_chat_send;
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position).fCommtenterId.equals(userId)) {
            return 1;
        }
        return super.getItemViewType(position);
    }

    /**
     * user id
     *
     * @param userId user id
     * @param userAvatar userAvatar
     */
    public void setUserId(String userId, String userAvatar) {
        this.userId = userId;
        this.userAvatar = userAvatar;
        notifyDataSetChanged();
    }

    class FromChatHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_chat_from_avatar)
        ImageView ivItemChatFromAvatar;
        @BindView(R.id.tv_item_chat_from_content)
        TextView tvItemChatFromContent;

        public FromChatHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            ChatListBean.PageBean.ListBean listBean = getItem(position);
            tvItemChatFromContent.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fComment));
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(userAvatar)).apply(new RequestOptions().circleCrop()
                    .placeholder(R.drawable.icon_default_avatar)
                    .error(R.drawable.icon_default_avatar)
            ).into(ivItemChatFromAvatar);
        }
    }

    class SendChatHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_chat_send_avatar)
        ImageView ivItemChatSendAvatar;
        @BindView(R.id.tv_item_chat_send_content)
        TextView tvItemChatSendContent;

        public SendChatHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            ChatListBean.PageBean.ListBean listBean = getItem(position);
            tvItemChatSendContent.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fComment));
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(UserManager.getInstance().getUserAvatar())).apply(new RequestOptions().circleCrop()
                    .placeholder(R.drawable.icon_default_avatar)
                    .error(R.drawable.icon_default_avatar)
            ).into(ivItemChatSendAvatar);
        }
    }
}
